import { _decorator, Component, Node, UITransform, Vec3, MotionStreak, EventTouch, Tween, director } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 水果忍者：启动入口
 */
@ccclass('gameFruitStart')
export class gameFruitStart extends Component {

    /**
     * 水果刀-拖影
     */
    @property(Node)
    private knifeNode: Node = null;

    /**
     * 开始按钮-外层圆圈
     */
    @property(Node)
    private newGameCircleNode: Node = null;
    /**
     * 开始按钮-内层水果
     */
    @property(Node)
    private newGameFruitNode: Node = null;

    /**
     * 离开按钮-外层圆圈
     */
    @property(Node)
    private quitGameCircleNode: Node = null;
    /**
     * 离开按钮-内层炸弹
     */
    @property(Node)
    private quitGameBoomNode: Node = null;

    private knifeMotionS: MotionStreak;

    onLoad() {
        this.knifeMotionS = this.knifeNode.getComponent(MotionStreak);
        console.log("onLoad");
    }

    start() {
        this.knifeMove();
        this.circleRotate();
        console.log("start");
    }

    circleRotate() {
        let createRote = (angle) => {
            return new Tween().by(7, { angle: angle }).repeatForever();
        }
        //外层节点的圈逆时针旋转
        new Tween(this.newGameCircleNode)
            .then(createRote(360))
            .start();

        new Tween(this.quitGameCircleNode)
            .then(createRote(360))
            .start();

        //内层节点顺时针旋转
        new Tween(this.newGameFruitNode)
            .then(createRote(-360))
            .start();

        new Tween(this.quitGameBoomNode)
            .then(createRote(-360))
            .start();
    }

    knifeMove() {
        this.node.on(Node.EventType.TOUCH_START, this.touchStartEvent, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
    }

    touchMoveEvent(e: EventTouch) {
        let transform = this.node.getComponent(UITransform);
        let pos = transform.convertToNodeSpaceAR(new Vec3(e.getLocationX(), e.getLocationY()));
        this.knifeNode.setPosition(pos);

        console.log("touchMoveEvent:{}", e);
    }

    touchStartEvent(e: EventTouch) {
        let transform = this.node.getComponent(UITransform);
        let pos = transform.convertToNodeSpaceAR(new Vec3(e.getLocationX(), e.getLocationY()));
        this.knifeNode.setPosition(pos);
        this.knifeMotionS.reset();

        //console.log("touchStartEvent");
    }

    update(deltaTime: number) {

    }

    /**
     * 点击开始按钮
     */
    clickGameStartBtn() {
        director.loadScene('gameFruitMain');
    }
}